package com.itheima.tilas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.itheima.tilas.Utils.JwtUtil;
import com.itheima.tilas.Utils.ResultUtil;
import com.itheima.tilas.entity.Emp;
import com.itheima.tilas.mapper.EmpMapper;
import com.itheima.tilas.po.LoginParam;
import com.itheima.tilas.service.LoginService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author 132
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    EmpMapper empMapper;

    @Override
    public ResultUtil selectEmpByUsername(LoginParam param) {
        Emp emp = empMapper.selectEmpByUsername(param.getUsername());
        if (Objects.isNull(emp) || !emp.getPassword().equals(param.getPassword())) {
            return ResultUtil.fail("用户名或密码输入错误");
        }
        String token = JwtUtil.generateToken(emp.getUsername(), emp.getPassword(), 300);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", emp.getId());
        jsonObject.put("username", emp.getUsername());
        jsonObject.put("name", emp.getName());
        jsonObject.put("token", token);
        return ResultUtil.success(jsonObject);
    }
}
